home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* text3D.c - show how to render moving and stationary text in
- * 3D scenes
- *
- * Escape key - exit the program
- * Left Mouse Button - change incidence and azimuth angles
- * Middle Mousebutton - change the twist angle based on
- * horizontal mouse movement
- * Right Mousebutton - zoom in and out based on vertical
- * mouse movement
- * R Key - reset viewpoint
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h> /* for fmodf */
- #include <stdio.h> /* for printf */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid animate( GLvoid );
- GLvoid visibility( GLint );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid mouse( GLint, GLint, GLint, GLint );
- GLvoid motion( GLint, GLint );
-
- void resetView( GLvoid );
- void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLfloat shoulderAngle = 0.0; /* controls shoulder rotation */
-
- static enum actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
- static GLint action;
-
- static GLdouble xStart = 0.0, yStart = 0.0;
-
- static GLfloat beamWidth = 2.0, beamHeight = 0.4, beamDepth = 1.0;
- static GLfloat near, far, distance, twistAngle, incAngle, azimAngle;
-
- static void *fixedFont, *strokeFont;
- static char *labelStr = "Robot Arm";
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( (width / 2) + 4, height / 4 );
- glutInitWindowSize( (width / 2) - 4, height / 2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutMouseFunc( mouse );
- glutMotionFunc( motion );
- glutKeyboardFunc( keyboard );
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - render a scene with both stationary "
- "and moving text\n\n"
- "Axes: X - red, Y - green, Z - blue\n\n"
- "Left Mousebutton - move eye position\n"
- "Middle Mousebutton - change twist angle\n"
- "Right Mousebutton - move up / down to zoom in / out\n"
- "Escape Key - exit the program\n"
- "<R> Key - reset viewpoint\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- GLfloat maxObjectSize;
-
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- glShadeModel( GL_FLAT );
- glEnable( GL_DEPTH_TEST );
-
- /* Maximum size of all the objects in your scene */
- maxObjectSize = fsqrt( ((2*beamWidth) * (2*beamWidth)) +
- ((2*beamHeight) * (2*beamHeight)) +
- (beamDepth * beamDepth) );
-
- /* Set up near and far so that ( far - near ) > maxObjectSize, */
- /* and determine the viewing distance (adjust for zooming) */
- near = 1.0;
- far = near + 8*maxObjectSize;
-
- resetView();
-
- /* Set up two fonts to use for rendering */
- fixedFont = GLUT_BITMAP_TIMES_ROMAN_24;
- strokeFont = GLUT_STROKE_ROMAN;
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, near, far );
- glMatrixMode( GL_MODELVIEW );
- }
-
- GLvoid
- animate( GLvoid )
- {
- /* update the rotation of the shoulder for each scene */
- shoulderAngle = fmodf( (shoulderAngle + 1.0), 360.0 );
-
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'R':
- resetView();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- static GLint buttons_down = 0;
-
- if (state == GLUT_DOWN) {
- switch (button) {
- case GLUT_LEFT_BUTTON:
- action = MOVE_EYE;
- break;
- case GLUT_MIDDLE_BUTTON:
- action = TWIST_EYE;
- break;
- case GLUT_RIGHT_BUTTON:
- action = ZOOM;
- break;
- }
-
- /* Update the saved mouse position */
- xStart = x;
- yStart = y;
- } else {
- if (--buttons_down == 0)
- action = MOVE_NONE;
- }
-
- }
-
- GLvoid
- motion( GLint x, GLint y )
- {
- switch (action) {
- case MOVE_EYE:
- /* Adjust the eye position based on the mouse position */
- azimAngle += (GLdouble) (x - xStart);
- incAngle -= (GLdouble) (y - yStart);
- break;
- case TWIST_EYE:
- /* Adjust the eye twist based on the mouse position */
- twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
- break;
- case ZOOM:
- /* Adjust the eye distance based on the mouse position */
- distance -= (GLdouble) (y - yStart)/10.0;
- break;
- default:
- printf("unknown action %d\n", action);
- }
-
- /* Update the stored mouse position for later use */
- xStart = x;
- yStart = y;
-
- glutPostRedisplay();
- }
-
- void
- resetView( GLvoid )
- {
- distance = near + (far - near) / 2.0;
- twistAngle = 0.0; /* rotation of viewing volume (camera) */
- incAngle = 0.0;
- azimAngle = 0.0;
- }
-
- void
- polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
- GLfloat twist)
- {
- glTranslatef( 0.0, 0.0, -distance);
- glRotatef( -twist, 0.0, 0.0, 1.0);
- glRotatef( -incidence, 1.0, 0.0, 0.0);
- glRotatef( -azimuth, 0.0, 0.0, 1.0);
- }
-
- GLvoid
- renderBitmapString( void *font, char *string )
- {
- int i;
- int len = (int) strlen(string);
- for (i = 0; i < len; i++) {
- glutBitmapCharacter(font, string[i]);
- }
- }
-
- GLvoid
- renderStrokeString( void *font, char *string )
- {
- int i;
- int len = (int) strlen(string);
- for (i = 0; i < len; i++) {
- glutStrokeCharacter(font, string[i]);
- }
- }
-
- /* print mouse and keyboard input commands */
- GLvoid
- printInputCommands( void *font )
- {
- static char *lmouse = "left mouse - change incidence & azimuth";
- static char *mmouse = "middle mouse - change twist";
- static char *rmouse = "right mouse - zoom in and out";
- static char *Rkey = "R key - reset view";
-
- GLfloat yoffset;
- GLsizei winHeight = glutGet(GLUT_WINDOW_HEIGHT);
-
- yoffset = winHeight-15.0;
- glRasterPos3f( 10.0, yoffset, 0.0 );
- renderBitmapString( font, lmouse );
-
- yoffset -= 20.0;
- glRasterPos3f( 10.0, yoffset, 0.0 );
- renderBitmapString( font, mmouse );
-
- yoffset -= 20.0;
- glRasterPos3f( 10.0, yoffset, 0.0 );
- renderBitmapString( font, rmouse );
-
- yoffset -= 20.0;
- glRasterPos3f( 10.0, yoffset, 0.0 );
- renderBitmapString( font, Rkey );
- }
-
- /* update status of polarView parameters */
- GLvoid
- printStatus( void *font )
- {
- static char s[50];
- GLfloat yoffset = 65.0;
-
- /* update polarview information */
- glRasterPos3f( 10.0, yoffset, 0.0 );
- sprintf (s, "incidence is %4d", (GLint) incAngle);
- renderBitmapString( font, s );
-
- yoffset -= 20.0;
- glRasterPos3f( 10.0, yoffset, 0.0 );
- sprintf (s, "azimuth is %4d", (GLint) azimAngle);
- renderBitmapString( font, s );
-
- yoffset -= 20.0;
- glRasterPos3f( 10.0, yoffset, 0.0 );
- sprintf (s, "Twist is %4d", (GLint) twistAngle);
- renderBitmapString( font, s );
- }
-
- GLvoid
- renderStationaryText( void *font )
- {
- GLsizei winWidth, winHeight;
-
- winWidth = glutGet(GLUT_WINDOW_WIDTH);
- winHeight = glutGet(GLUT_WINDOW_HEIGHT);
-
- /* Turn off the depth buffer */
- glDisable( GL_DEPTH_TEST );
-
- /* save and then clear modelview matrix */
- glPushMatrix();
- glLoadIdentity();
-
- /* save Projection matrix */
- glMatrixMode( GL_PROJECTION );
- glPushMatrix();
- /* reset world space to display status */
- glLoadIdentity();
- glOrtho( 0.0, winWidth, 0.0, winHeight, -1.0, 1.0 );
-
- printInputCommands( font );
- printStatus( font );
-
- /* restore projection matrix */
- glPopMatrix();
-
- /* restore previous modelview transformations */
- glMatrixMode( GL_MODELVIEW );
- glPopMatrix();
-
- /* Turn on the depth buffer */
- glEnable( GL_DEPTH_TEST );
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- static GLfloat upperArmColor[] = { 1.0, 0.0, 0.0 };
- static GLfloat lowerArmColor[] = { 0.8, 0.5, 0.5 };
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
- polarView( distance, azimAngle, incAngle, twistAngle );
- XYZaxes();
-
- /* Draw the shoulder centered at the new origin */
- glRotatef( shoulderAngle, 0.0, 0.0, 1.0 );
- glTranslatef( 1.0, 0.0, 0.0 );
- glColor3fv( upperArmColor );
- WireBox( beamWidth, beamHeight, beamDepth );
-
- /* Draw the lower arm */
- glTranslatef( 1.0, 0.0, 0.0 );
- glRotatef( 45.0, 0.0, 0.0, 1.0 );
- glTranslatef( 1.0, 0.0, 0.0 );
- glColor3fv( lowerArmColor );
- WireBox( beamWidth, beamHeight, beamDepth );
-
- glColor3f( 1.0, 1.0, 1.0 );
-
- glTranslatef( 1.5, 1.8, 0.0 );
- glRotatef( -90.0, 0.0, 0.0, 1.0 );
- glScalef( 0.005, 0.005, 0.005 );
- renderStrokeString( strokeFont, labelStr );
- glPopMatrix();
-
- glColor3f( 1.0, 1.0, 0.0 );
- renderStationaryText( fixedFont );
-
- glutSwapBuffers();
- }
-